package cn.rcth.base.serivce.impl;

import cn.rcth.base.VO.VerifyCodeVO;
import cn.rcth.base.constants.BaiseConstants;
import cn.rcth.base.domain.User;
import cn.rcth.base.domain.VerifyCode;
import cn.rcth.base.enums.ErrorCode;
import cn.rcth.base.mapper.UserMapper;
import cn.rcth.base.msg.WxOpenMsg;
import cn.rcth.base.serivce.IUserSerivce;
import cn.rcth.base.serivce.IVerifyCodeSerivce;
import cn.rcth.base.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class UserSerivceImpl implements IUserSerivce {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SendVerifyCodeServiceImpl sendVerifyCodeService;

    @Autowired
    private IVerifyCodeSerivce verifyCodeSerivce;

    @Override
    /**
     *  查询数据库是否存在当前用户并将用户默认数据存入数据库
     */
    public User ifUser(WxOpenMsg openMsg) {
        User user = userMapper.selectOpenId(openMsg.getOpenId());
        Date date = new Date();
        if (user == null && openMsg.getOpenId() != null) {
            User user1 = new User();
            //用户不存在,存入初始数据
            user1.setOpenId(openMsg.getOpenId());
            user1.setCreateTime(date);
            user1.setLoginTime(date);
            //保存数据库
            userMapper.insert(user1);
            return user1;
        }else {
            user.setLoginTime(date);
            userMapper.updateByPrimaryKey(user);
        }
        return user;

    }


    @Override
    public void ifInsertUser(String openId, String phone) {
        User user = userMapper.selectOpenId(openId);
        user.setPhoneNumber(phone);
        userMapper.updateByPrimaryKey(user);
        //发送用户注册成功信息
        System.out.println("注册成功");
        //sendVerifyCodeService.send(user.getPhoneNumber(),BaiseConstants.TRUE_CONTEXT);
    }

    /**
     * 验证 验证码和手机号是否一致
     *
     * @param verifyCodeVO 验证信息
     * @return 对错
     */
    @Override
    public boolean ifVerifyCode(VerifyCode verifyCode) {
        //断言手机号是否为空
        Assert.isPhone(verifyCode.getPhone(), ErrorCode.REGISTER_USERNAME_ERROR.getErrorMsg());
        //从redis中获取验证信息
        //VerifyCodeVO verifyCode = (VerifyCodeVO) redisTemplate.opsForValue().get(BaiseConstants.verifyCode + verifyCodeVO.getPhone());
        VerifyCode verifyCodes = verifyCodeSerivce.getCodeByPhone(verifyCode.getPhone());
        String code = verifyCode.getCode();
        String phone = verifyCode.getPhone();
        if (verifyCode.getCode().equals(code) && verifyCode.getPhone().equals(phone)) {
            //删除数据库的验证吗
            verifyCodeSerivce.deleteCode(verifyCodes.getId());
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User ifPhone(String phone) {

        return userMapper.ifPhone(phone);
    }

    @Override
    public void saveUser(Long userId, Integer gender, String nickName, String heodImg ) {
        if (userId != null){
            User user = userMapper.selectByPrimaryKey(userId);
            user.setNickName(nickName);
            user.setGender(gender);
            user.setHeodImg(heodImg);
            user.setModifyTime(new Date());
            user.setModifyId(userId);
            userMapper.updateByPrimaryKey(user);

        }


    }

    @Override
    public void savePhone(Long userId ,String phone) {
        Assert.isPhone(phone, ErrorCode.REGISTER_USERNAME_ERROR.getErrorMsg());
        if (userId != null){
            User user = userMapper.selectByPrimaryKey(userId);
            user.setPhoneNumber(phone);
            user.setModifyTime(new Date());
            user.setModifyId(user.getId());
            userMapper.updateByPrimaryKey(user);
        }

    }

    @Override
    public User getUser(Long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

}
